package com.base.cn.platform.os.controller.manage.loreset;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.web.LoginUtil;
import com.base.cn.platform.os.service.manage.loreset.LoresetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台知识体系Controller
 *
 * @author s.li
 * @create 2018-03-23 10:21
 */
@Controller
@RequestMapping("/admin/manage/loreset")
public class AdminLoresetController extends AdminBaseController {

    @Autowired
    private LoresetService loresetService;
    @Autowired
    private LoginUtil loginUtil;

    /**
     * 查看知识体系详情
     * @param id 体系ID
     * @return 显示页面
     */
    @RequestMapping("/loresetInfo")
    public String loresetInfo(Model model ,@RequestParam("id") BigDecimal id){
        Map<String,Object> loreset = loresetService.findLoresetById(id,true,true,true);
        model.addAttribute("loreset",loreset);
        return resultPage("/manage/loreset/lore-info");
    }

    /**
     * 初始化知识体系列表页面
     * @param model
     * @return 显示页面
     */
    @RequestMapping("/queryLoresetListPage")
    public String queryLoresetListPage(Model model){
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> dataMap = loresetService.findLoresetListPage(params,1,false,false,false);
        Object count = dataMap.get("total");
        model.addAttribute("count",count);
        return resultPage("/manage/loreset/lore-list");
    }

    /**
     * ajax获取知识体系数据
     * @param model
     * @param loreset
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajax/ajaxQueryLoresetList")
    public String ajaxQueryLoresetList(Model model,
                                       @RequestBody(required = false) Map<String,Object> loreset,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> dataMap = loresetService.findLoresetListPage(loreset,currentPage,true,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/manage/loreset/ajax-lore-list");
    }

    /**
     * 初始化添加/修改页面
     * @param model
     * @param id 知识体系ID（修改时传）
     * @return 编辑页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id",required = false,defaultValue = "0")BigDecimal id){
        Map<String,Object> loreset = null;
        if(id.compareTo(new BigDecimal(0)) >0){
            loreset = loresetService.findLoresetById(id,true,false,false);
        }else{
            loreset = new HashMap<>();
        }
        model.addAttribute("loreset",loreset);
        return resultPage("/manage/loreset/save-lore");
    }

    /**
     * 保存知识点数据
     * @param loreset 知识点数据对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveLoreset")
    @ResponseBody
    public Map<String,Object> saveLoreset(HttpServletRequest request,@RequestBody Map<String, Object> loreset){
        loreset.put("createUserId",loginUtil.getLoginUserId(request));
        return loresetService.saveLoreset(loreset);
    }

    /**
     * 修改知识体系状态
     * @param loresetIds 知识体系ID串
     * @param status 知识体系状态，1正常，2冻结
     * @return Map<String,Object>
     */
    @RequestMapping("/updateLoresetStatus")
    @ResponseBody
    public Map<String,Object> udpateLoresetStatus(@RequestParam("loresetIds") String loresetIds,
                                                  @RequestParam("status") int status){
        return loresetService.updateLoresetStatus(loresetIds,status);
    }
    /**
     * 获取知识体系
     * @param subjectIds 专业ID串
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/findLoreset")
    @ResponseBody
    public List<Map<String,Object>> findLoreset(@RequestParam("subjectIds") String subjectIds){
        return loresetService.findLayeredSysExamLoresetPointList(subjectIds,false);
    }
}
